﻿using Apps.BLL.HC;
using Apps.Common;
using Apps.Locale;
using Apps.Models;
using Apps.Web.Core;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace Apps.Web.Areas.HC.Controllers
{
    public class HC_FIELD_REFERENCE_VALUEController : BaseController
    {
        HC_ITEMS_BLL bll = new HC_ITEMS_BLL();
        HC_EXTEND_FIELD_BLL fieldBll = new HC_EXTEND_FIELD_BLL();
        HC_ITEM_EXTEND_REL_BLL relBll = new HC_ITEM_EXTEND_REL_BLL();
        ValidationErrors errors = new ValidationErrors();

        [SupportFilter]
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        [SupportFilter(ActionName = "Index")]
        public JsonResult GetList()
        {
            int totalCount = 0;
            string queryStr = Utils.ObjectToStr(Request["queryStr"]);
            int pageSize = Utils.ObjToInt(Request["rows"], 15);
            int pageIndex = Utils.ObjToInt(Request["page"], 1);
            string sort = Utils.ObjectToStr(Request["sort"]);
            string order = Utils.ObjectToStr(Request["order"]);
            List<HC_ITEMS> list = bll.GetList(queryStr, pageIndex, pageSize, sort, order, ref totalCount);
            return Json(new Pagination.PaginationResult<HC_ITEMS>(totalCount, list.ToList()));
        }
        #region 创建
        [SupportFilter]
        public ActionResult Create()
        {
            List<HC_EXTEND_FIELD> fieldList = fieldBll.GetList();
            ViewBag.FieldList = fieldList;
            return View();
        }

        [HttpPost]
        [SupportFilter]
        public JsonResult Create(HC_ITEMS model)
        {
            string fieldIds = Utils.ObjectToStr(Request["fields"]);
            if (model != null && ModelState.IsValid)
            {
                if (bll.AddIsRepeated(model.ITEM_NAME))
                {
                    return Json(JsonHandler.CreateMessage(0, "已存在相同化产项目名称!!"));
                }
                if (fieldIds.Length > 0)
                {
                    List<HC_ITEM_EXTEND_REL> relList = new List<HC_ITEM_EXTEND_REL>();
                    foreach (var item in fieldIds.Split(','))
                    {
                        relList.Add(new HC_ITEM_EXTEND_REL()
                        { ID = ResultHelper.NewId, EXTEND_FIELD_ID = item, ITEM_ID = model.ID }
                        );
                    }
                    model.fieldList = relList;
                }
                string jsonstr = Utils.ToJson(model, Formatting.Indented);
                //if (bll.DoAdd(model))
                //{
                //    LogHandler.WriteServiceLog(GetUserId(), jsonstr, "成功", "创建", "HC_HC_ITEMS");
                //    return Json(JsonHandler.CreateMessage(1, Resource.InsertSucceed));
                //}
                //else
                //{
                //    string ErrorCol = errors.Error;
                //    LogHandler.WriteServiceLog(GetUserId(), jsonstr, "失败", "创建", "HC_HC_ITEMS");
                //    return Json(JsonHandler.CreateMessage(0, Resource.InsertFail + ErrorCol));
                //}
                return Json(JsonHandler.CreateMessage(0, Resource.InsertFail));
            }
            else
            {
                return Json(JsonHandler.CreateMessage(0, Resource.InsertFail));
            }
        }
        #endregion
    }
}